package com.yun.partnerSys.controller;

import com.yun.partnerSys.common.BaseResponse;
import com.yun.partnerSys.common.ResultUtils;
import com.yun.partnerSys.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {

    @Resource
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    public BaseResponse<String> upload(MultipartFile file){

        try {
            String originalFilename = file.getOriginalFilename();
            int index = originalFilename.lastIndexOf(".");
            //文件后缀
            String extension = originalFilename.substring(index);
            String fileName = UUID.randomUUID().toString() + extension;

            String filePath = aliOssUtil.upload(file.getBytes(), fileName);
            return ResultUtils.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败{}",e);
        }
        return null;
    }
}
